package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @aothor LentDent1
 * @vision 1.0
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        //创建ShoppingCart 拷贝数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        // 每个用户只能查看自己的购物车 设置id //如何获取自己的i的？
        shoppingCart.setUserId(BaseContext.getCurrentId());

        // 判断商品是否在购物车上了 先从数据库中获取信息
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);

        if (shoppingCarts != null && shoppingCarts.size() == 1){
            // 如果已经存在了，number加一
            shoppingCart = shoppingCarts.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            // 更新数据库
            shoppingCartMapper.updateNumberId (shoppingCart);
        } else {
            // 如果不存在，那么number数量就是1

            // 判断当前的cart是套餐还是dish
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null ) {
                // 是普通餐品 设置其他值
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                // 是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            // shoppingCart对象配置完成 导入数据库
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    @Override
    public List<ShoppingCart> list() {

        // 获取用户的id
        Long id = BaseContext.getCurrentId();

        // 构建ShoppingCart模板作为查询条件
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .id(id)
                .build();
        // 查询返回
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);

        return shoppingCarts;
    }

    @Override
    public void clean() {

        // 调用mapper
        shoppingCartMapper.deleteByUserId (BaseContext.getCurrentId());
    }
}
